//
//  BbsListCtrl.m
//  ClienHD
//
//  Created by Sangwoo Park on 11. 1. 24..
//  Copyright 2011 Student. All rights reserved.
//

#import "BbsListCtrl.h"
#import "ConfigScrlViewCtrl.h"
#import "ThreadListViewCtrl.h"

@implementation BbsListCtrl

@synthesize BBS1, BBS2, LOGIN, BBS1On, BBS2On;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	[self setTitle:@"Clien.net"];
	
	UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithTitle:@"설정"
																   style:UIBarButtonItemStyleBordered
																  target:self
																  action:@selector(menuButtonPressed)];
	self.navigationItem.rightBarButtonItem = menuButton;
	[menuButton autorelease];

	//Document에 소모임 plist가 있으면 있는것을 사용 없으면 MainBundle에서 복사
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *docPath = [paths objectAtIndex:0];
	NSString *plistPath = [docPath stringByAppendingPathComponent:@"BbsList.plist"];

	if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath] == NO) {
		NSString *srcPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"BbsList.plist"];
		[[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:plistPath error:nil];
	}

	NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
	
	BBS1 = [[dic objectForKey:@"BBS1"] mutableCopy];
	BBS2 = [[dic objectForKey:@"BBS2"] mutableCopy];
	LOGIN = [[dic objectForKey:@"LOGIN"] mutableCopy];

	BBS1On = [[NSMutableArray alloc] init];
	BBS2On = [[NSMutableArray alloc] init];

	[self makeListBbsOn];

	[dic release];
}



- (void)viewWillAppear:(BOOL)animated {
	[self.tableView reloadData];

//	[super viewWillAppear:animated];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Override to allow orientations other than the default portrait orientation.
    return YES;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (section == 0) return [BBS1On count];
	else return [BBS2On count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	if (section == 0) return @"게시판";
	return @"소모임";
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
	NSArray *item;
    if (indexPath.section == 0) {
		item = [BBS1On objectAtIndex:indexPath.row];
	}
	else {
		item = [BBS2On objectAtIndex:indexPath.row];
	}

	cell.textLabel.text = [item objectAtIndex:0];
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

#pragma mark -
#pragma mark TableView delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.

    ThreadListViewCtrl *threadListVC = [[ThreadListViewCtrl alloc] init];
	NSArray *item;
	if (indexPath.section == 0) {
		item = [BBS1On objectAtIndex:indexPath.row];
	}
	else {
		item = [BBS2On objectAtIndex:indexPath.row];
	}

	[threadListVC setTitle:[item objectAtIndex:0]];
	threadListVC.bbsAddr = [item objectAtIndex:1];

	[self.navigationController pushViewController:threadListVC animated:YES];

	[threadListVC LoadBbsIndex:1];

	[threadListVC autorelease];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[BBS1 release];
	[BBS2 release];
	[LOGIN release];
	[BBS1On release];
	[BBS2On release];

    [super dealloc];
}

#pragma mark -

- (IBAction)menuButtonPressed {
	ConfigScrlViewCtrl *configVC = [[ConfigScrlViewCtrl alloc] initWithTitle:@"설정"
																	delegate:self bbs1Array:BBS1 bbs2Array:BBS2 login:LOGIN];

	[self.navigationController pushViewController:configVC animated:YES];
	
	[configVC autorelease];
}

#pragma mark -

- (void)makeListBbsOn {
	NSNumber * visible;
	[BBS1On removeAllObjects];

	for (NSArray *item in BBS1) {
		visible = [item objectAtIndex:2];
		if ([visible boolValue] == YES) {
			[BBS1On addObject:item];
		}
	}

	[BBS2On removeAllObjects];
	
	for (NSArray *item in BBS2) {
		visible = [item objectAtIndex:2];
		if ([visible boolValue] == YES) {
			[BBS2On addObject:item];
		}
	}
	
	[self.tableView reloadData];
}

@end

